<?php
class TagsController extends Controller{
	
	public function actionList(){
		$tags = Tags::model()->findAllBySql("SELECT * FROM {{tags}} ORDER BY tags_id DESC");
		$this->render('list', array('tags'=>$tags));
	}
	
	public function actionAdd(){
		$model = new Tags();
		if(isset($_POST['Tags'])){
			$model->attributes = $_POST['Tags'];
			if($model->save()){
				$this->redirect(array('list'));
			}
		}
		$this->render('add', array('model'=>$model));
	}
	
	public function actionEdit($tagsId){
		$tagsId = intval($tagsId);
		if($tagsId <= 0){
			$this->redirect(array('list'));
		}
		
		$model = Tags::model();
		$model = $model->findByPk($tagsId);
		if(isset($_POST['Tags'])){
			$model->attributes = $_POST['Tags'];
			if($model->save()){
				$this->redirect(array('list'));
			}
		}
		$this->render('edit', array('model'=>$model));
	}
	
	public function actionDelete(){
		if(Yii::app()->request->isAjaxRequest){
			$tagsId = Yii::app()->request->getPost('tagsId');
		}
		
		//删除tags对应记录
		$res1 = Tags::model()->deleteByPk($tagsId);
		
		//删除article_tags表对应记录
		$res2 = Yii::app()->db->createCommand()->delete('{{article_tags}}', 'tags_id = :tagsId', array(':tagsId'=>$tagsId));
		
		if($res1 || $res2){
			die(json_encode(array('code'=>1)));
		} else {
			die(json_encode(array('code'=>0)));
		}
	}
}